from flask import Flask, jsonify, abort, request
import time
import threading
from datetime import datetime, timedelta
import uuid

app = Flask(__name__)

class mock:
    @staticmethod
    def launch_dlc_internal():
        import tkinter as tk
        
        root = tk.Tk()
        root.title("DeepLabCut GUI")
        root.geometry("800x600")
        
        # 创建一个标签
        label = tk.Label(root, text="DeepLabCut GUI 模拟界面")
        label.pack(pady=20)
        
        # 创建一个按钮
        button = tk.Button(root, text="关闭", command=root.destroy)
        button.pack(pady=10)
        
        root.mainloop()

import deeplabcut
dlc = deeplabcut

status_dict = {}
result_dict = {}

@app.route('/')
def index():
    return 'local GUI for deeplabcut'

@app.route('/meta')
def meta():
    return jsonify({
        'name': 'deeplabcut[gui]',
        'description': 'local GUI for deeplabcut',
        'category': 'tracking',
    })

@app.route('/tasks')
def get_tasks():
    return jsonify([
        'main: FilePath -> Map<Str, List<List<(PointF, Float)>>>',
        'gui: Void -> Void',
        'test: Void -> Void'
    ])

@app.route('/workflows')
def get_workflows():
    return jsonify([])

@app.route('/mount', methods=['GET'])
def mount():
    return jsonify({
        'session_key': str(uuid.uuid4()),
        'healthy_info': 'Healthy'
    })

@app.route('/task/main', methods=['POST'])
def start_task():
    session_key = request.headers.get('session_key')
    global status_dict
    id = str(uuid.uuid4())
    status_dict[id] = {
        'status': 'Running',
        'percentage': 0
    }
    result_dict[id] = None
    def run_task(task_id: str):
        status_dict[task_id]['percentage'] = 50
        dlc.launch_dlc_internal()
        status_dict[task_id]['status'] = 'Completed'
        status_dict[task_id]['percentage'] = 100
        result_dict[task_id] = 'success'
    task_thread = threading.Thread(target=run_task, args=(id,))
    task_thread.daemon = True
    task_thread.start()
    return jsonify({
        'task_id': id,
        'progress_pipe_addr': f'/task/main/progress/{id}',
        'result_addr': f'/task/main/result/{id}'
    })

@app.route('/task/main/progress/<task_id>')
def get_progress(task_id):
    global status_dict
    if task_id not in status_dict:
        abort(400, 'task not found')
    return jsonify(status_dict[task_id])

@app.route('/task/main/result/<task_id>')
def get_result(task_id):
    global result_dict
    if task_id not in result_dict:
        abort(400, 'task not found')
    return jsonify(result_dict[task_id])

@app.route('/task/test', methods=['POST'])
def start_test():
    id = str(uuid.uuid4())
    return jsonify({
        'task_id': id,
        'progress_pipe_addr': f'/task/test/progress/{id}',
        'result_addr': f'/task/test/result/{id}'
    })

@app.route('/task/test/progress/<task_id>')
def get_test_progress(task_id):
    return jsonify({
        'status': 'Completed',
        'percentage': 100
    })

@app.route('/task/test/result/<task_id>')
def get_test_result(task_id):
    return jsonify(None)

if __name__ == '__main__':
    app.run(host='127.0.0.1', port=10000, debug=True)
